// import axios form 'axios';
// import router from '../router'

// const request = axios.create({
//     baseURL: 'http://localhost:3000',
//     timeout:5000
// })


// //请求拦截器
// request.interceptors.request.use(
//     config => {
//         const token = localStorage.getItem('token')
//         if(token) {
//             console.log('发送请求：', {
//                 url:config.url,
//                 method:config.method,
//                 headers: {Authorization: `Bearer ${token}`}
//             })
//             config.headers.Authorization = `Bearer ${token}`
//         } else {
//             console.log('未找到token')
//         }

//         return config
//     },

//     error => {
//         return Promise.reject(error)
//     }
// )


// //响应拦截器

// request.interceptors.response.use(
//     response => {
//         console.log('接受到的响应',response)
//         return response.data
//     },
//     error => {
//         console.error('请求错误：',error.response)
//         if(error.response?.status === 401) {
//             localStorage.removeItem('token')
//             localStorage.removeItem('user')
//             router.push('/login')

//         }
//         return Promise.reject(error.response?.data?.error || '服务器错误')
//     }
// )

// export default request






    import {ref,computed} from 'vue'
    import {useRouter} from 'vue-router'
    import request from '@utils/request'
    import { showToast } from 'vant'

    const router =  useRouter()
    const isRegister = ref(false)
    const countDown = ref(0)
    const loading = ref(false)


//登入表单数据

const loginForm = ref({
    email:'',
    password:''
})

const registerForm = ref({
    email:'',
    password:'',
    code:''
})

// 验证码倒计时

const isCountDown = computed(() => countDown.value > 0)
const countDownText = computed(() => isCountDown.value ? `{countDown.value}s` : '获取验证码')

const validateEmail = (email) => {
    const reg  = /^[6\s@]+@[^\s@]+\.[^\s@]+$/
    return reg.test(email)
}



//检查邮箱是否已经注册
const checkEmail = async(email) => {
    try {
        const response = await request.post('/api/check-email',{email});
        return response.exists;
    }
    catch(error){
        console.error('检查邮箱失败：',error);
        return false;
    }
}

//发送验证码
const sendVerificationCode = async () => {
    if(!registerForm.value.email) {
        showToast('请输入邮箱')
        return
    }
    if(!validateEmail(registerForm.value.email)){
        showToast('请输入正确的邮箱格式')
        return
    }

    try {
        loading.value = true

        const emailExists = await checkEmail(registerForm.value.email);
        if(emailExists){
            showToast('该邮箱已被注册');
            return
        }

        await request.post('api/send-code',{
            email:registerForm.value.email
        })

        showToast('验证码已发送')
        countDown.value = 60
        const timer  = setInterval(() => {
            countDown.value--
            if(countDown.value <= 0) {
                clearInterval(timer)
            }
        },1000)
    }   catch (error) {
        if(error?.response?.data?.error === '该邮箱已被注册') {
            showToast('该邮箱已被注册');
        }
        else {
            showToast(error || '发送验证码失败');
        }
    }

    finally {
        loading.value = false
    }
   
    
  
}

